import http from '../../utils/request'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        mhList:[],
        count:'',
        userInfo:wx.getStorageSync('userInfo')?JSON.parse(wx.getStorageSync('userInfo')):'',
        page:1,
        pageSize:15,
        isShow:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.clearCache();//清本页缓存
        this.myupload(1)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.clearCache();//清本页缓存
        this.myupload(1)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let {page,loadMore}=this.data
        let that = this
        if (!loadMore) {
            that.setData({
                loadMore: true, //加载中  
                loadAll: false //是否加载完所有数据
            });
            //加载更多，这里做下延时加载
            setTimeout(function() {
                that.myupload(page);
            }, 1000)
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    // 清缓存
   clearCache:function(){
    this.setData({
        page:1,
        mhList: [] //文章列表数组清空
    }); 
},
    //点击资源跳转详情
    linkDetail(e){
        let title = e.currentTarget.dataset.name
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/pages/resoucedetail/resoucedetail?title='+title+'&id='+id+'&myupload=1'
        });
    },
    async myupload(){
        let that = this
        // let {userInfo} = that.data
        var {page,pageSize} = this.data;
        let options={
                url:'/getRecord',
                method:'POST',
                data:{ page:page,pagesize:pageSize,userid:JSON.parse(wx.getStorageSync('userInfo')).id,authorization:wx.getStorageSync('userInfo')?JSON.parse(wx.getStorageSync('userInfo')).authorization:null},
                header:{"content-type":"application/json;charset=UTF-8"}
            }
        let res = await http(options)
        console.info(res)
        if(res.data.code==0){
            if (res.data.obj && res.data.obj.rrs.length > 0) {
                let {page,mhList,pageSize} = that.data
                page++
                // 这一步实现了上拉加载更多
                res.data.obj.rrs.map(item=>{
                    let extea = item.itemName.substring(item.itemName.lastIndexOf('.')+1).toLowerCase()
                    let timedata = item.createDate.substring(0,10)
                    return Object.assign(item,{suffix:extea,timedata:timedata})
                })
                let tmpArr = mhList.concat(res.data.obj.rrs)
                that.setData({
                    mhList: tmpArr,
                    page:page,
                    loadMore: false,
                    count:res.data.obj.count
                })
                if (res.data.obj.rrs.length < pageSize) {
                    that.setData({
                      loadMore: false, //隐藏加载中。。
                      loadAll: true //所有数据都加载完了
                    });
                  }
            }else{
                that.setData({
                    loadAll: true, //把“没有数据”设为true，显示  
                    loadMore: false //把"上拉加载"的变量设为false，隐藏  
                  });
            }
        }else{
            if(res.data.code==5){
                that.setData({isShow:true})
            }else{
                wx.showToast({
                    title: res.data.msg,
                    icon: 'error',
                    duration: 2000
                })
            }
        }
       
        // that.setData({mhList:res.data.obj.rrs,count:res.data.obj.count})
    }
})